package cn.xmy.demo01.servlet;
import cn.xmy.demo01.pojo.Weapon;
import cn.xmy.demo01.service.WeaponService;
import cn.xmy.demo01.service.impl.WeaponServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/allWeaponServlet")
public class AllWeaponServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据
        String star=request.getParameter("star");
        String equip=request.getParameter("equip");
        String attribute=request.getParameter("attribute");
        String obtain=request.getParameter("obtain");
        WeaponService weaponService=new WeaponServiceImpl();
        try {
            List<Weapon> weapons=weaponService.findByWeapon(star,equip,attribute,obtain);
            String a= JSON.toJSONString(weapons);
            PrintWriter out=response.getWriter();
            out.println(a);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
